%define debug_package %{nil}
%define name scratch-desktop

%global __requires_exclude ^lib(ffmpeg|EGL|GLES|vk_swiftshader|vulkan)
%global __provides_exclude ^lib.*

Name: %{name}
Version: 3.30.5
Release: 1

Group: Education
License: BSD-3-Clause
Url: https://scratch.mit.edu/

Summary: Scratch-desktop is a set of React components that comprise the interface for creating and running Scratch 3.0 projects
Summary(ru.UTF-8): Scratch-desktop - это набор компонентов React, которые составляют интерфейс для создания и запуска проектов Scratch 3.0

Source: https://github.com/scratchfoundation/%{name}/archive/refs/tags/v%{version}.tar.gz?/%{name}-%{version}.tar.gz

# predownload node_modules
# $ npm run fetch

# $ vim scripts/electron-builder-wrapper.js
#--- a/scripts/electron-builder-wrapper.js	2023-08-15 16:13:45.000000000 +0300
#+++ b/scripts/electron-builder-wrapper.js	2023-10-03 07:29:39.367486449 +0300
#@@ -145,6 +145,8 @@
#         }
#         targets.push(availableTargets.macDirectDownload);
#         break;
#+    case 'linux':
#+        break;
#     default:
#         throw new Error(`Could not determine targets for platform: ${process.platform}`);
#     }

# $ npm install --save-dev file-loader

# #Module not found: Error: Can't resolve '../generated/microbit-hex-url.cjs'

# $ cd node_modules/scratch-gui
# $ npm install html-webpack-plugin
# $ npm install uglifyjs-webpack-plugin
# $ npm run build:dir

# $ date=$(date +%Y%m%d)
# $ XZ_OPT="-6 --threads=0 -v" tar cJf %%{name}-nodejs-modules-${date}.tar.xz node_modules
Source1: %{name}-nodejs-modules-20231003.tar.xz

# predownload static assets
# $ npm run fetch
# $ date=$(date +%Y%m%d)
# $ XZ_OPT="-6 --threads=0 -v" tar cJf %%{name}-static-${date}.tar.xz static
Source2: %{name}-static-20231003.tar.xz
Source3: scratch-desktop.desktop
Patch1: 0001-Fix-package-build.patch
Patch2: 0002-Fix-launch-application.patch

ExclusiveArch: x86_64

BuildRequires: nodejs
BuildRequires: pkgconfig(xshmfence)
BuildRequires: nss-devel
BuildRequires: at-spi2-core-devel
BuildRequires: libdrm-devel
BuildRequires: gtk+3-devel
BuildRequires: pkgconfig(alsa)

%description
Scratch-desktop is a standalone Electron based desktop application.
Scratch is a new programming language that makes it easy to create your own
interactive stories, animations, games, music, and art -- and share your
creations on the web.

Scratch is designed to help young people (ages 8 and up) develop 21st century
learning skills. As they create Scratch projects, young people learn important
mathematical and computational ideas, while also gaining a deeper understanding
of the process of design.

%description -l ru_RU.UTF-8
Scratch-desktop - отдельное настольное приложение на базе Electron.
Scratch - это новый язык программирования, который позволяет легко создавать 
свои собственные интерактивные истории, анимацию, игры, музыку и искусство 
- и делиться своими творениями в Интернете.

Scratch разработан, чтобы помочь молодым людям (в возрасте 8 лет и старше) 
развить навыки обучения 21-го века. При создании Scratch-проектов молодые люди 
изучают важные математические и вычислительные идеи, а также получают более 
глубокое понимание процесса проектирования.

%files
%doc LICENSE TRADEMARK README.md
%{_bindir}/%{name}
%{_datadir}/applications/%{name}.desktop
%{_iconsdir}/hicolor/*/apps/*
%{_libdir}/%{name}/

#-------------------------------------------------------------------------------
%prep
%setup -q -a1 -a2
%patch1 -p1
%patch2 -p1

%build
npm run build:dir

mkdir -p asar_resources/node_modules/buffer-from/
mkdir -p asar_resources/node_modules/source-map-support/
mkdir -p asar_resources/node_modules/source-map/
mkdir -p asar_resources/static/assets/
mkdir -p asar_resources/static/blocks-media/

cp -a dist/main/*  asar_resources/
cp -a dist/renderer/*  asar_resources/

cp -a dist/renderer/static/*  asar_resources/static/
cp -a node_modules/buffer-from/*  asar_resources/node_modules/buffer-from/
cp -a node_modules/source-map-support/*  asar_resources/node_modules/source-map-support/
cp -a node_modules/source-map/*  asar_resources/node_modules/source-map/
cp -a package.json asar_resources/

node_modules/asar/bin/asar.js pack asar_resources/ app.asar

%install
install -D -m644 src/icon/ScratchDesktop.png %{buildroot}%{_iconsdir}/hicolor/1024x1024/apps/ScratchDesktop.png
install -D -m644 src/icon/ScratchDesktop.svg %{buildroot}%{_iconsdir}/hicolor/scalable/apps/ScratchDesktop.svg
install -D -m644 %{SOURCE3} %{buildroot}%{_datadir}/applications/%{name}.desktop

mkdir -p %{buildroot}%{_libdir}/%{name}/resources/static/

cp -a node_modules/electron/dist/*  %{buildroot}%{_libdir}/%{name}/
rm -f %{buildroot}%{_libdir}/%{name}/resources/default_app.asar
install -D -m755 app.asar %{buildroot}%{_libdir}/%{name}/resources/app.asar

mkdir -p %{buildroot}/%{_bindir}/
ln -rs %{buildroot}/%{_libdir}/%{name}/electron %{buildroot}/%{_bindir}/%name

cp -a static/* %{buildroot}%{_libdir}/%{name}/resources/static/
